package com.scau.yangsuiyu.estore.controller.seller.manageShop;

import com.scau.yangsuiyu.estore.dababases.dao.ShopDao;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import com.scau.yangsuiyu.estore.dababases.entity.Shop;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 卖家店铺基本信息
 * @date 2019-06-09 00:22
 * @email suiyu_yang@163.com
 */
public class InfoShopSeller extends AnchorPane implements Initializable {
    private Seller seller;
    private Shop shop;

    @FXML
    private Label sellerUsernameLabel;

    @FXML
    private TextField shopNameTF;

    @FXML
    private TextField businessLicenseTF;

    @FXML
    private TextArea profileTA;

    @FXML
    private Label quantityScoreLabel;

    @FXML
    private Label serviceScoreLabel;

    @FXML
    private Label deliveryScoreLabel;

    public InfoShopSeller(Seller seller) {
        this.seller = seller;
        this.shop = seller.getShop();
        loadFXML();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        sellerUsernameLabel.setText(seller.getUsername());

        shopNameTF.setText(shop.getShopName());
        businessLicenseTF.setText(shop.getBusinessLicense());
        profileTA.setText(shop.getProfile());
        deliveryScoreLabel.setText(shop.getDeliveryScore() + "");
        quantityScoreLabel.setText(shop.getQualityScore() + "");
        serviceScoreLabel.setText(shop.getServiceScore() + "");
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/seller/manageShop/InfoShop.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void saveButtonClick() {
        setUIShopMessage();
        seller.setShop(shop);
        new ShopDao().update(shop);
        new Alert(Alert.AlertType.INFORMATION, "保存成功！");
    }

    private void resetTFAndTA() {
        sellerUsernameLabel.setText(seller.getUsername());
        shopNameTF.setText(shop.getShopName());
        businessLicenseTF.setText(shop.getBusinessLicense());
        profileTA.setText(shop.getProfile());
        deliveryScoreLabel.setText(shop.getDeliveryScore() + "");
        quantityScoreLabel.setText(shop.getQualityScore() + "");
        serviceScoreLabel.setText(shop.getServiceScore() + "");
    }

    /**
     * 将界面的值赋予给Shop
     */
    private void setUIShopMessage() {
        if (shopNameTF.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入店铺名！").showAndWait();
        } else if (businessLicenseTF.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入营业执照编号！").showAndWait();
        } else if (profileTA.getText() == null) {
            new Alert(Alert.AlertType.ERROR, "请输入商铺简介！").showAndWait();
        } else {
            String shopNameTFText = shopNameTF.getText();
            if (!shopNameTFText.equals(shop.getShopName())) {
                Shop tempShop = new ShopDao().getShopByName(shopNameTFText);
                if (tempShop == null) {
                    shop.setShopName(shopNameTFText);
                    shop.setBusinessLicense(businessLicenseTF.getText());
                    shop.setProfile(profileTA.getText());

                } else {
                    new Alert(Alert.AlertType.ERROR, "店铺名已存在！").showAndWait();
                    resetTFAndTA();
                }
            } else {
                shop.setShopName(shopNameTFText);
                shop.setBusinessLicense(businessLicenseTF.getText());
                shop.setProfile(profileTA.getText());

            }
        }
    }
}
